﻿#region Copyright ©2008-2009, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-03-19 13:58:26 +0100 (Thu, 19 Mar 2009) $
// Last Changed Revision $Rev: 42 $
//
// ====================================================================================================
#endregion

using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using Tum.CollabXT.TFS.Properties;

namespace Tum.CollabXT.TFS
{
    /// <summary>
    /// Interaction logic for TFSConfigPage.xaml
    /// </summary>
    public partial class TFSConfigPage : ToolProviderConfigPage
    {
        TFSToolProvider ToolProvider;
        IConversionWorkflow ConversionWorkflow;


        public TFSConfigPage()
        {
            InitializeComponent();
        }

        public override void ReceiveProviderHandle(IToolProvider toolProvider)
        {
            ToolProvider = toolProvider as TFSToolProvider;

            if (Directory.Exists(Settings.Default.LastTemplatePath))
                ToolProvider.TemplatePath = Settings.Default.LastTemplatePath;

            SetValues();
        }

        public override void ReceiveWorkflowHandle(IConversionWorkflow workflow)
        {
            ConversionWorkflow = workflow;

            UpdateWorkflowButtons();
        }

        private void UpdateWorkflowButtons()
        {
            if(ToolProvider != null)
            {
                ConversionWorkflow.UpdateButtonState(true, ToolProvider.OutputPath != null && ToolProvider.TemplatePath != null);
            }
        }

        private void SetValues()
        {
            //Output path
            if (ToolProvider.OutputPath == null)
                labelOutputPathValue.Content = TFS.Resources.Language.PathNotSet;
            else
            {
                labelOutputPathValue.Content = ToolProvider.OutputPath;
                labelOutputPathValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }

            //Template path
            if (ToolProvider.TemplatePath == null)
                labelTemplatePathValue.Content = TFS.Resources.Language.PathNotSet;
            else
            {
                labelTemplatePathValue.Content = ToolProvider.TemplatePath;
                labelTemplatePathValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }

            //WSS dump path
            if (ToolProvider.SharePointDumpPath == null)
               labelWSSDumpPathValue.Content = TFS.Resources.Language.PathNotSet;
            else
                labelWSSDumpPathValue.Content = ToolProvider.SharePointDumpPath;

            //Button states
            UpdateWorkflowButtons();
        }

        private void buttonSetOutputPath_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = true;

            if (Directory.Exists(Settings.Default.LastOutputPath))
                fbd.SelectedPath = Settings.Default.LastOutputPath;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                ToolProvider.OutputPath = fbd.SelectedPath;
                ConversionWorkflow.InvalidateProcessing();
                SetValues();

                Settings.Default.LastOutputPath = fbd.SelectedPath;
                Settings.Default.Save();
            }
        }

        private void buttonSetWSSDumpPath_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;

            if (Directory.Exists(Settings.Default.LastSharePointDumpPath))
                fbd.SelectedPath = Settings.Default.LastSharePointDumpPath;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                ToolProvider.SharePointDumpPath = fbd.SelectedPath;
                ConversionWorkflow.InvalidateProcessing();
                SetValues();

                Settings.Default.LastSharePointDumpPath = fbd.SelectedPath;
                Settings.Default.Save();
            }
        }

        private void buttonSetTemplatePath_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;

            if (Directory.Exists(Settings.Default.LastTemplatePath))
                fbd.SelectedPath = Settings.Default.LastTemplatePath;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                ToolProvider.TemplatePath = fbd.SelectedPath;
                ConversionWorkflow.InvalidateProcessing();
                SetValues();

                //Save path as 'last template path'
                Settings.Default.LastTemplatePath = ToolProvider.TemplatePath;
                Settings.Default.Save();
            }
        }
    }
}
